package com.fh.shop.api.recipient.biz;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.common.SystemConst;
import com.fh.shop.api.recipient.mapper.IRecipientMapper;
import com.fh.shop.api.recipient.po.Recipient;
import com.fh.shop.api.util.KeyUtil;
import com.fh.shop.api.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;;

import javax.annotation.Resource;
import java.util.List;

@Service("recipientService")
@Transactional(rollbackFor = Exception.class)
public class IRecipientServiceImpl implements IRecipientService {
    @Resource
    private IRecipientMapper recipientMapper;

    //添加订单信息
    @Override
    public ServerResponse addRecipient(Recipient recipient) {
        //非空验证
        //电话验证
        recipientMapper.insert(recipient);
        //清缓存
        Long memberId = recipient.getMemberId();
        RedisUtil.delete(KeyUtil.buildRecipientKey(memberId));
        return ServerResponse.success();
    }

    //查询收件人信息
    @Override
    public ServerResponse findList(Long id) {
        //每次先从缓存服务器查找
        String key = KeyUtil.buildRecipientKey(id);
        String recipientJson = RedisUtil.get(key);
        if (StringUtils.isNotEmpty(recipientJson)){
            List<Recipient> recipientList = JSON.parseArray(recipientJson, Recipient.class);
            return ServerResponse.success(recipientList);
        }
        //查找数据库
        QueryWrapper<Recipient> recipientQueryWrapper = new QueryWrapper<>();
        recipientQueryWrapper.eq("memberId",id);
        List<Recipient> recipients = recipientMapper.selectList(recipientQueryWrapper);
        //将查找到的数据，往缓存中存一份
        String str = JSON.toJSONString(recipients);
        RedisUtil.set(key,str);
        //响应前台
        return ServerResponse.success(recipients);
    }

    //更新收件人信息,是否是默认地址
    @Override
    public ServerResponse updateStatus(Long id,Long memberId) {
        //现将当前会员所有的收件人默认地址状态重置为0
        Recipient recipients = new Recipient();
        recipients.setStatus(SystemConst.RECIPIENT_ADDR_STATUS_NOT_DEFAULT);
        QueryWrapper<Recipient> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("memberId",memberId);
        recipientMapper.update(recipients, updateWrapper);
        //更新收件人默认地址状态
        Recipient recipient = new Recipient();
        recipient.setId(id);
        recipient.setStatus(SystemConst.RECIPIENT_ADDR_STATUS_DEFAULT);
        recipientMapper.updateById(recipient);
        //清缓存
        RedisUtil.delete(KeyUtil.buildRecipientKey(memberId));
        return ServerResponse.success();
    }

    //删除收件人信息
    @Override
    public ServerResponse deleteRecipient(Long id,Long memberId) {
        recipientMapper.deleteById(id);
        //删除redid缓存
        RedisUtil.delete(KeyUtil.buildRecipientKey(memberId));
        return ServerResponse.success();
    }

    //回填收件人信息
    @Override
    public ServerResponse findRecipient(Long id) {
        Recipient recipient = recipientMapper.selectById(id);
        return ServerResponse.success(recipient);
    }

    //修改收件人信息
    @Override
    public ServerResponse updateRecipient(Recipient recipient) {
        recipientMapper.updateById(recipient);
        //清缓存
        Long memberId = recipient.getMemberId();
        RedisUtil.delete(KeyUtil.buildRecipientKey(memberId));
        return ServerResponse.success();
    }

}
